home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / VideoToolbox 96.06.15 / VideoToolboxSources / Identify.c < prev    next >
Text File  |  1996-05-29  |  33KB  |  951 lines

  1. /* NameRegistryLib is in the VideoToolbox Libs folder. */
  2. //#define NO_NAME_REGISTRY_LIB 1    /* Uncomment this if you're missing Apple's NameRegistryLib */
  3. /*
  4. Identify.c
  5.  
  6. NOTE: several routines that use Apple's NameRegistryLib appear at the end of this file.
  7. If you don't have that file, then un-comment the first line above.
  8.  
  9. Each routine returns an informative C string. Here are samples. (I suggest you
  10. use BreakLines() to printf the longer ones.)
  11.  
  12. IdentifyCompiler:
  13. "Compiled by Metrowerks CodeWarrior C 7.00 for PowerPC, 4B int and 8B double."
  14.  
  15. IdentifyOwner:
  16. "Denis Pelli"
  17.  
  18. IdentifyModel:
  19. "PowerBook 170"
  20.  
  21. IdentifyMachine:
  22. "Denis Pelli's PowerBook 170, 68030, 68882, System 7.1.0."
  23. "Mal's PowerMac 8500/120, ppc604, L2 cache, System 7.5.2."
  24.  
  25. IdentifyVideo(device):
  26. "PowerBook 170 \"Macintosh D Built-In Video\" (.Display_Video_Apple_TIM) in slot 0"
  27. "PowerMac 8500/120 \"Built-in video\" (.Display_Video_Apple_Control)"
  28.  
  29. The computer model appears only if the slot==0, which indicates built-in video. 
  30. Quotes embrace the card name, and parentheses embrace the driver name.
  31. The driver's version number appears only if it's non-zero.
  32.  
  33. HISTORY:
  34. 1/29/92    dgp wrote IdentifyCompiler.
  35. 2/25/92    dgp    wrote IdentifyMachine.
  36. 8/26/92    dgp get owner and model name from the System file
  37. 2/20/93    dgp    added ROM version
  38. 2/27/93    dgp merged IdentifyCompiler.c and IdentifyMachine.c into the new Identify.c,
  39.             and added IdentifyVideo.
  40. 4/25/93    dgp    IdentifyMachine reports 24/32-bit addressing.
  41. 12/14/93 dgp If THINK C compiler version is "5" I report "5 or 6.0" since Symantec
  42.             forgot to increment the version number when they released 6.0. They
  43.             fixed this in version 6.01.
  44. 3/2/94    dgp    Extracted IdentifyOwner from within IdentifyMachine
  45. 7/28/94 dgp added support for Metrowerks CodeWarrior C compiler and PowerPC.
  46.             Eliminated use of "#s" printf format, since it's not supported by
  47.             Metrowerks CodeWarrior C.
  48. 9/5/94 dgp     removed assumption in printf's that int==short.
  49. 9/6/94    dgp    Enhanced IdentifyCompiler to specify the size of int.
  50. 9/6/94    dgp    Disable cache checks on PowerPC, because those traps are undefined.
  51. 10/5/94 dgp Removed all floating point code from IdentifyCompiler().
  52. 11/5/94 dgp Suppressed ROM info in Identify.
  53. 11/9/94 dgp added IdentifyApplication().
  54. 1/15/95 dgp added test for version 5 of CodeWarrior
  55. 5/24/95 dgp added test for version 6 of CodeWarrior
  56. 6/14/95 dgp added GetCPUSpeed() to IdentifyMachine() and abbreviated the report, 
  57.         omitting a lot of boring stuff.
  58. 6/27/95 dgp adjusted UNIVERSAL_HEADERS conditional for GMT struct, to use old
  59.         structure for version 1 universal headers.
  60. 7/20/95    dgp once again, made compatible with pre-universal headers
  61. 8/10/95 dgp updated IdentifyCompiler() for Symantec C.
  62. 9/26/95 dgp updated IdentifyCompiler() for CW7.
  63. 10/5/95 dgp minor editing of IdentifyVideo().
  64. 10/14/95 dgp added support for PCI Macs, using the NameRegistry. Since System 7.5 fails to
  65.         adequately name most Macs, I've reverted to using a built-in table of names in 
  66.         IdentifyModel. IdentifyMachine now also prints the Gestalt machine number to 
  67.         resolve any residual ambiguities.
  68.         IdentifyModel, IdentifyVideo, and IdentifyMachine now give complete information on all
  69.         Macs, provided that when running on PCI Macs they are compiled as PPC code. This 
  70.         limitation is due to the (easy) way that I implemented the calls to Apple's 
  71.         NameRegistryLib. The NameRegistry-related routines appear in a long self-contained
  72.         section at the end of this file.
  73. 10/24/95 dgp added pclk and bclk Gestalt calls, based on gestalt-selectors-31.
  74. 12/4/95 dgp As requested by David Brainard, changed "char *machineName[]" to 
  75.         "char machineName[][32]" so that it would compile as a code resource.
  76. 3/7/96 dgp report system version number in standard  format, e.g. "7.5.3".
  77. 3/18/96 dgp IdentifyModel() now always reports actual clock speed if known.
  78. 5/14/96 dgp Added mach codes for new Macs from GestaltSelectorList 3.3.5.
  79. 5/28/96 dgp Added conditional UNIVERSAL_INTERFACES_VERSION>=0x0212
  80. */
  81. #include "VideoToolbox.h"
  82. #ifndef __TRAPS__
  83.     #include <Traps.h>        // _HWPriv
  84. #endif
  85. #include <Power.h>
  86. #if UNIVERSAL_HEADERS
  87.     #include <LowMem.h>
  88. #else
  89.     #define LMGetCurApName() ((StringPtr) 0x0910)
  90.     #define gestaltNativeCPUtype 'cput'
  91.     enum{
  92.         gestaltCPU601                = 0x101,
  93.         gestaltCPU603                = 0x103,
  94.         gestaltCPU604                = 0x104
  95.     };
  96.     enum {
  97.         gestaltSysArchitecture        = 'sysa',    /* Native System Architecture */
  98.         gestalt68k                    = 1,        /* Motorola MC68k architecture */
  99.         gestaltPowerPC                = 2            /* IBM PowerPC architecture */
  100.     };
  101.     #pragma parameter __D0 MaximumProcessorSpeed
  102.     extern pascal short MaximumProcessorSpeed(void)={0x7010, 0xA09E};
  103. #endif
  104. void GetCPUProperties(char *cpuName,long *cpuHz,Boolean *hasL2Cache);
  105. void GetVideoProperties(GDHandle device,char *slotName,char *cardName,char *modelName);
  106. char *GetDeviceSlotName(GDHandle device);
  107. #define gestaltBusClockSpeed 'bclk'    /* gestalt-selectors-31.etx */
  108. #define gestaltCPUClockSpeed 'pclk'    /* gestalt-selectors-31.etx */
  109.  
  110. // System 7.5 took a step backwards and lacks
  111. // specific names for many machines, so I used Rene Ros's Gestalt Selectors List
  112. // to list all currently available Macs.
  113. char machineName[][36]={
  114.     "Unknown Mac"
  115.     ,"Mac 128K"
  116.     ,"Mac XL"
  117.     ,"Mac 512KE"
  118.     ,"Mac Plus"
  119.     ,"Mac SE"
  120.     ,"Mac II"
  121.     ,"Mac IIx"
  122.     ,"Mac IIcx"
  123.     ,"Mac SE-30"
  124.     ,"Mac Portable"
  125.     ,"Mac IIci"
  126.     ,"Unknown Mac"    /* 12 */
  127.     ,"Mac IIfx"
  128.     ,"Unknown Mac"
  129.     ,"Unknown Mac"
  130.     ,"Unknown Mac"
  131.     ,"Mac Classic","Mac IIsi","Mac LC","Mac Quadra 900","PowerBook 170"
  132.     ,"Mac Quadra 700","Mac Classic II","PowerBook 100","PowerBook 140","Quadra 950"
  133.     ,"Mac LC III"
  134.     ,"Unknown Mac"
  135.     ,"Mac PowerBook 210"
  136.     ,"Mac Centris 650"
  137.     ,"Unknown Mac"
  138.     ,"Mac PowerBook 230"
  139.     ,"Mac PowerBook 180"
  140.     ,"Mac PowerBook 160"
  141.     ,"Mac Quadra 800"    /* 35 */
  142.     ,"Mac Quadra 650"
  143.     ,"Mac LC II"
  144.     ,"Mac PowerBook Duo 250"
  145.     ,"PowerMac 9150"
  146.     ,"PowerMac 8100/110"    /* 40 */
  147.     ,"PowerMac 5200"    /* 41 */
  148.     ,"Unknown Mac"
  149.     ,"Unknown Mac"
  150.     ,"Mac IIvi"
  151.     ,"Mac Performa 600"
  152.     ,"Unknown Mac"
  153.     ,"PowerMac 7100/80"    /* 47 */
  154.     ,"Mac IIvx"
  155.     ,"Mac Color Classic"
  156.     ,"Mac PowerBook 165c"
  157.     ,"Unknown Mac"
  158.     ,"Mac Centris 610"
  159.     ,"Mac Quadra 610"
  160.     ,"Mac PowerBook 145,145b"
  161.     ,"PowerMac 8100/100"    /* 55 */
  162.     ,"Mac LC 520"
  163.     ,"Unknown Mac"
  164.     ,"Unknown Mac"
  165.     ,"Unknown Mac"
  166.     ,"Mac Centris 660AV"
  167.     ,"Unknown Mac"
  168.     ,"Mac Performa 46x"
  169.     ,"Unknown Mac"
  170.     ,"Unknown Mac"
  171.     ,"PowerMac 8100/80"    /* 65 */
  172.     ,"Unknown Mac"
  173.     ,"PowerMac 9500"    /* 67 */
  174.     ,"PowerMac 7500,7600"    /* 68 */
  175.     ,"PowerMac 8500"    /* 69 */
  176.     ,"Unknown Mac"
  177.     ,"Mac PowerBook 180c"
  178.     ,"Mac PowerBook 500,520,520c,540,540c" /* 72 */
  179.     ,"Unknown Mac"
  180.     ,"Unknown Mac"
  181.     ,"PowerMac 6100/60"    /* 75 */
  182.     ,"Unknown Mac"
  183.     ,"Mac PowerBook Duo 270c"
  184.     ,"Mac Quadra 840AV"
  185.     ,"Unknown Mac"
  186.     ,"Mac LC/Performa 550"
  187.     ,"Unknown Mac"
  188.     ,"Unknown Mac"
  189.     ,"Unknown Mac"
  190.     ,"Mac PowerBook 165"
  191.     ,"Unknown Mac"
  192.     ,"Unknown Mac"
  193.     ,"Unknown Mac"
  194.     ,"Mac TV"
  195.     ,"Mac LC/Performa 475"
  196.     ,"Unknown Mac"
  197.     ,"Unknown Mac"
  198.     ,"Mac LC 575"
  199.     ,"Unknown Mac"
  200.     ,"Mac Quadra 605"
  201.     ,"Unknown Mac"
  202.     ,"Unknown Mac"
  203.     ,"Unknown Mac"
  204.     ,"Mac LC/Quadra 630"
  205.     ,"Unknown Mac"
  206.     ,"PowerMac 6100/66"    /* 100 */
  207.     ,"PowerMac 6100"    /* 80 MHz, discovered by clock chippers */
  208.     ,"Mac PowerBook Duo 280"
  209.     ,"Mac PowerBook Duo 280c"
  210.     ,"PowerMac LC/Performa 475"
  211.     ,"PowerMac LC/Performa 575"
  212.     ,"PowerMac LC/Quadra 630"
  213.     ,"Unknown Mac"
  214.     ,"PowerMac 7200"    /* 108, also PowerMac 8200/100 */
  215.     ,"Unknown Mac"
  216.     ,"Unknown Mac"
  217.     ,"Unknown Mac"
  218.     ,"PowerMac 7100/66"    /* 112 */
  219.     ,"PowerMac 7100"    /* 80 MHz, discovered by clock chippers */
  220.     ,"Unknown Mac"
  221.     ,"Mac PowerBook 150"
  222.     ,"PowerMac Quadra700"
  223.     ,"PowerMac Quadra900"
  224.     ,"PowerMac Quadra950"
  225.     ,"PowerMac Centris610"
  226.     ,"PowerMac Centris650"
  227.     ,"PowerMac Quadra610"
  228.     ,"PowerMac Quadra650"
  229.     ,"PowerMac Quadra800"
  230.     ,"PowerBook 2300c"    /* 124 */
  231.     ,"Unknown Mac"
  232.     ,"Unknown Mac"
  233.     ,"Unknown Mac"
  234.     ,"PowerBook 5300"
  235. };
  236.  
  237. char fpuName[][32]={
  238.     "no floating point unit","68881","68882","built-in floating point"
  239.     ,"unknown floating point unit","Software FPU"
  240. };
  241.  
  242. char *IdentifyCompiler(void)
  243. {
  244.     static char string[200];
  245.     char *compiler,*longs,*floating,version[4],*universal;
  246.     double v=0;
  247.     Boolean fractionalVersion=0;
  248.     
  249.     string[0]=0;
  250.     compiler="";
  251.     #if THINK_C && !SYMANTEC_C
  252.         compiler="Symantec THINK C";
  253.         if(THINK_C==1)v=4;
  254.         else v=THINK_C;
  255.     #endif
  256.     #if THINK_CPLUS || SYMANTEC_C
  257.         #if __cplusplus || THINK_CPLUS
  258.             compiler="Symantec C++";
  259.         #else
  260.             compiler="Symantec C";
  261.         #endif
  262.         #if THINK_CPLUS
  263.             v=THINK_CPLUS;
  264.         #else
  265.             fractionalVersion=1;
  266.             v=SYMANTEC_C/0x100 + 0.1*(SYMANTEC_C/0x10%0x10) + 0.01*(SYMANTEC_C%0x10);
  267.         #endif
  268.     #endif
  269.     #if applec
  270.         compiler="MPW C";
  271.     #endif
  272.     #if __MWERKS__
  273.         compiler="Metrowerks CodeWarrior C";
  274.         #if __MWERKS__>1
  275.             fractionalVersion=1;
  276.             v=__MWERKS__/0x100 + 0.1*(__MWERKS__/0x10%0x10) + 0.01*(__MWERKS__%0x10);
  277.         #else
  278.             #if defined(__MC68K__)||defined(__POWERPC__)||defined(__INTEL__)
  279.                 v=6;
  280.             #else
  281.                 if(!iscntrl(0))v=4;    // bug in version 4.5 and earlier
  282.                 else v=5;
  283.             #endif
  284.         #endif
  285.     #endif
  286.     if(v>0){
  287.         if(fractionalVersion)sprintf(version,"%.2f ",v);
  288.         else sprintf(version,"%.0f ",v);
  289.     }else sprintf(version,"");
  290.     if(GENERATING68020)longs="68020";
  291.     else{
  292.         #if GENERATINGPOWERPC
  293.             longs="PowerPC";
  294.         #else
  295.             longs="68000";
  296.         #endif
  297.     }
  298.     if(GENERATING68881)floating=" and 68881";
  299.     else floating="";
  300.     universal="";
  301.     if(sizeof(double)==12){
  302.         #if THINK_C
  303.             #if !__option(native_fp)
  304.                 universal="\"universal\"-format ";
  305.             #endif
  306.         #endif
  307.     }
  308.     sprintf(string,"Compiled by %s %sfor %s%s, %ldB int and %s%ldB double."
  309.         ,compiler,version,longs,floating,sizeof(int),universal,sizeof(double));
  310.     return string;
  311. }
  312.  
  313. char *IdentifyModel(void)
  314. {
  315.     int error;
  316.     long machine=0,value;
  317.     int machines=sizeof(machineName)/sizeof(machineName[0]);
  318.     static char string[64];
  319.     char cpuName[32]="";
  320.     long cpuHz=0,busHz=0;
  321.     Boolean hasL2Cache,powerMgrExists;
  322.     
  323.     #if 0
  324.         /* Before System 7.5 the MacOS supplied the model name. Now it merely supplies a
  325.             generic name, like "Power Macintosh". */
  326.         if(!error){
  327.             GetIndString((unsigned char *)string,kMachineNameStrID,machine);
  328.             p2cstr((unsigned char *)string);
  329.             if(strlen(string)==0){
  330.                 if(machine<0 || machine>=machines)machine=0;
  331.                 sprintf(string,"%s",machineName[machine]);
  332.             }
  333.         }
  334.     #endif
  335.     /* The model names in the machineName[] table include clock speed, where appropriate,
  336.     for the pre-PCI Macs, e.g. 6100/60, but omit clock speed for the PCI Macs, since 
  337.     PCI Macs must run System 7.5.2 or better, which include gestaltCPUClockSpeed, whereas
  338.     pre-PCI Macs may be running 7.5.1 which did not include gestaltCPUClockSpeed. 
  339.     If the actual clock speed is available we always use it, overwriting the nominal speed if
  340.     necessary. */
  341.     error=Gestalt(gestaltMachineType,&machine);
  342.     strcpy(string,machineName[machine]);
  343.     GetCPUProperties(cpuName,&cpuHz,&hasL2Cache);    // useful only on PCI Macs
  344.     error=Gestalt(gestaltBusClockSpeed,&busHz);        // requires System 7.5.2 or better
  345.     error=Gestalt(gestaltCPUClockSpeed,&cpuHz);        // requires System 7.5.2 or better
  346.     if(cpuHz==0){
  347.         Gestalt(gestaltPowerMgrAttr,&value);        // Only on powerbooks I think.
  348.         powerMgrExists=value&(1L<<gestaltPMgrExists);
  349.         if(powerMgrExists)cpuHz=1000000*GetCPUSpeed();
  350.     }
  351.     if(cpuHz>0){
  352.         int i=strlen(string);
  353.         // remove the nominal speed, if present, before appending the actual speed
  354.         if(i>4 && string[i-4]=='/' && isdigit(string[i-3]) && isdigit(string[i-2]) && isdigit(string[i-1]))
  355.             string[i-4]=0;
  356.         if(i>3 && string[i-3]=='/' && isdigit(string[i-2]) && isdigit(string[i-1]))
  357.             string[i-3]=0;
  358.         sprintf(string,"%s/%ld",string,cpuHz/1000000);
  359.     }
  360.     return string;
  361. }
  362.  
  363. char *IdentifyVideo(GDHandle device)
  364. // E.g. "PowerBook 170 \"Macintosh D Built-In Video\" (.Display_Video_Apple_TIM)"
  365. {
  366.     static char string[256];
  367.     long quickDraw;
  368.     char *slotName;
  369.  
  370.     string[0]=0;
  371.     Gestalt(gestaltQuickdrawVersion,&quickDraw);
  372.     if(quickDraw<gestalt8BitQD){
  373.         sprintf(string,"%s ",IdentifyModel());
  374.         sprintf(string,"%s\"%s\"",string,"1-bit QuickDraw");
  375.     }else{
  376.         slotName=GetDeviceSlotName(device);
  377.         if(strlen(slotName)==0 || strcmp(slotName,"0")==0)sprintf(string,"%s ",IdentifyModel());
  378.         sprintf(string,"%s\"%s\"",string,GDCardName(device));
  379.         if(GDVersion(device)==0)sprintf(string,"%s (%s)",string,GDNameStr(device));
  380.         else sprintf(string,"%s (%s version %d)"
  381.             ,string,GDNameStr(device),(int)GDVersion(device));
  382.         if(strlen(slotName)>0 && strcmp(slotName,"0")!=0)sprintf(string,"%s slot %s",string,slotName);
  383.     }
  384.     return string;
  385. }
  386.  
  387. char *IdentifyOwner(void)
  388. {
  389.     static char string[64];
  390.     unsigned char **owner;
  391.     
  392.     string[0]=0;
  393.     owner=GetString(-16096);    // Get owner's name from System file
  394.     if(owner!=NULL && *owner[0]>0){
  395.         memcpy(string,*owner,(*owner)[0]+1);
  396.         p2cstr((unsigned char *)string);
  397.     }
  398.     return string;
  399. }
  400.  
  401. char *IdentifyApplication(void)    // Returns name of application.
  402. {
  403.     static char string[32];
  404.  
  405.     memcpy(string,LMGetCurApName(),32);
  406.     p2cstr((unsigned char *)string);
  407.     return string;
  408. }
  409.  
  410. char *IdentifyMachineAndType(void);
  411.  
  412. char *IdentifyMachineAndType(void)
  413. {
  414.     int error;
  415.     long machine=0;
  416.     char *s;
  417.  
  418.     s=IdentifyMachine();
  419.     error=Gestalt(gestaltMachineType,&machine);
  420.     sprintf(s,"%s GestaltMachineType %ld.",s,machine);
  421.     return s;
  422. }
  423.  
  424. char *IdentifyMachine(void)
  425. {
  426.     int error;
  427.     long fpu,processor=-1,system,value,sysArchitecture;
  428.     int fpus=sizeof(fpuName)/sizeof(fpuName[0]);
  429.     static char string[256];
  430.     Boolean cacheData=1,cacheInstructions=1;
  431.     long romSize,romVersion,qD;
  432.     char *processorName,*owner,fpuString[32];
  433.     char cpuName[32]="";
  434.     long cpuHz=0,busHz=0;
  435.     Boolean hasL2Cache;
  436.     char cache[32]="";
  437.     
  438.     GetCPUProperties(cpuName,&cpuHz,&hasL2Cache);    /* works only on PCI Macs */
  439.     error=Gestalt(gestaltBusClockSpeed,&busHz);        /* works only on PCI Macs */
  440.     error=Gestalt(gestaltCPUClockSpeed,&cpuHz);        /* works only on PCI Macs */
  441.     string[0]=0;
  442.     error=Gestalt(gestaltSystemVersion,&system);
  443.     if(error)return string;                        /* Gestalt not available */
  444.     sysArchitecture=gestalt68k;                    /* default */
  445.     error=Gestalt(gestaltSysArchitecture,&sysArchitecture);
  446.     switch(sysArchitecture){
  447.         case gestalt68k:
  448.             error=Gestalt(gestaltProcessorType,&processor);
  449.             switch(processor){
  450.                 case 1: processorName="68000";break;
  451.                 case 2: processorName="68010";break;
  452.                 case 3: processorName="68020";break;
  453.                 case 4: processorName="68030";break;
  454.                 case 5: processorName="68040";break;
  455.                 default: processorName="unknown processor";break;
  456.             }
  457.             break;
  458.         case gestaltPowerPC:
  459.             error=Gestalt(gestaltNativeCPUtype,&processor);
  460.             switch(processor){
  461.                 case gestaltCPU601: processorName="ppc601";break;
  462.                 case gestaltCPU603: processorName="ppc603";break;
  463.                  case 0x106:         processorName="ppc603e";break;/* PowerBook 5300 */
  464.                 case gestaltCPU604: processorName="ppc604";break;
  465.                 default: processorName="unknown processor";break;
  466.             }
  467.             break;
  468.         default:
  469.             processorName="unknown processor";
  470.             break;
  471.     }
  472.     Gestalt(gestaltFPUType,&fpu);
  473.     if(fpu<0 || fpu>=fpus)fpu=fpus-2;
  474.     if(fpu==1 && Gestalt('FPUE',&value)==0)fpu=fpus-1;// Software FPU by John Neil & Assoc.
  475.     if(fpu>0)sprintf(fpuString,", %s",fpuName[fpu]);
  476.     else fpuString[0]=0;
  477.     owner=IdentifyOwner();
  478.     if(strlen(owner)>0)sprintf(string,"%s's ",owner);
  479.     sprintf(string,"%s%s",string,IdentifyModel());
  480.     if(strlen(cpuName)>0){
  481.         if(hasL2Cache)strcpy(cache,", L2 cache");
  482.         else strcpy(cache,"");
  483.     }
  484.     sprintf(string,"%s, %s%s%s, System %lx.%lx"
  485.         ,string,processorName,cache,fpuString,system/0x100,system%0x100/0x10);
  486.     system%=0x10;
  487.     if(system)sprintf(string,"%s.%lx",string,system);
  488.     sprintf(string,"%s.",string);
  489.     if(0){    // The rest is boring, so I've disabled it.
  490.         Gestalt(gestaltROMSize,&romSize);
  491.         Gestalt(gestaltROMVersion,&romVersion);
  492.         sprintf(string,"%s, %ldK ROM version %ld+%ld*256."
  493.             ,string,romSize/1024,romVersion%256,romVersion/256);
  494.         if(TrapAvailable(_HWPriv)){
  495.             #if !GENERATINGPOWERPC    /* apparently these functions don't work on PowerPC */
  496.                 cacheData=SwapDataCache(1);
  497.                 SwapDataCache(cacheData);
  498.                 cacheInstructions=SwapInstructionCache(1);
  499.                 SwapInstructionCache(cacheInstructions);
  500.                 if(cacheData || cacheInstructions)sprintf(string,"%s Caching",string);
  501.                 else sprintf(string,"%s No caching",string);
  502.                 if(cacheData)sprintf(string,"%s data",string);
  503.                 if(cacheData && cacheInstructions)sprintf(string,"%s &",string);
  504.                 if(cacheInstructions)sprintf(string,"%s instructions",string);
  505.                 sprintf(string,"%s.",string);
  506.             #endif
  507.         }
  508.         value=0;
  509.         Gestalt(gestaltAddressingModeAttr,&value);
  510.         {
  511.             if(value&1L<<gestalt32BitAddressing)sprintf(string,"%s 32-bit addressing.",string);
  512.             else sprintf(string,"%s 24-bit addressing.",string);
  513.         }
  514.         Gestalt(gestaltQuickdrawVersion,&qD);
  515.         switch(qD/0x100){
  516.             case 0:
  517.                 sprintf(string,"%s 1-bit QuickDraw.",string);
  518.                 break;
  519.             case 1:
  520.                 sprintf(string,"%s 8-bit QuickDraw.",string);
  521.                 break;
  522.             case 2:
  523.                 sprintf(string,"%s 32-bit QuickDraw 1.%02lx.",string,qD%0x100);
  524.                 break;
  525.             default:
  526.                 sprintf(string,"%s QuickDraw 0x%lx.",string,qD);
  527.                 break;
  528.         }
  529.     }
  530.     return string;
  531. }
  532.  
  533. char *IdentifyVM(void)
  534. {
  535.     // Based on gestalt-selectors-29.etx, available from Info-Mac.
  536.     Boolean vm;            // VM is on, possibly due to RAM Doubler
  537.     Boolean ramDoubler;    // RAM Doubler is on.
  538.     long value;
  539.     int error;
  540.     char *s="";
  541.     
  542.     error=Gestalt(gestaltVMAttr,&value);
  543.     vm=!error && value&(1L<<gestaltVMPresent);
  544.     error=Gestalt('vmem',&value);
  545.     ramDoubler=!error && value=='RaM2';
  546.     if(vm){
  547.         if(!ramDoubler)s="Using Virtual Memory. ";
  548.         else s="Using RAM Doubler. ";
  549.     }
  550.     return s;
  551. }
  552.  
  553. char *IdentifyGreenwichMeanTime(void)
  554. {
  555.     MachineLocation location;
  556.     long t;
  557.     double hours;
  558.     static char s[64];
  559.  
  560.     ReadLocation(&location);
  561.     if(location.latitude!=0 || location.longitude!=0){
  562.         #if UNIVERSAL_HEADERS>1
  563.             t=location.u.gmtDelta & 0x00FFFFFF;
  564.         #else
  565.             t=location.gmtFlags.gmtDelta & 0x00FFFFFF;
  566.         #endif
  567.         if(t & 0x800000)t|=0xFF000000;
  568.         hours=fabs(t/3600.0);
  569.         if(hours==floor(hours))sprintf(s,"%.0f",hours);
  570.         else sprintf(s,"%.1f",hours);
  571.         if(t<0)sprintf(s,"%s hours west of Greenwich mean time.",s);
  572.         else sprintf(s,"%s hours east of Greenwich mean time.",s);
  573.     }else s[0]=0;
  574.     return s;
  575. }
  576.  
  577. /************************************************************************/
  578.  
  579. /*
  580. Using Apple's NameRegistry
  581.  
  582. void GetVideoProperties(GDHandle device,char *slotName,char *cardName,char *cardModel);
  583. void GetCPUProperties(char *cpuName,long *cpuHz,Boolean *hasL2Cache);
  584.  
  585. Two routines that use the new Name Registry (available only on PCI PowerMacs) to
  586. determine the cpu clock rate, presence of L2 cache, and the name of the PCI video
  587. card. The routines return zeroes if the Name Registry is not available (i.e. on
  588. non-PCI Macs), or if compiled as 68k code.
  589.  
  590. WARNING: to effectively use Apple's NameRegistry in your project you MUST have Apple's
  591. NameRegistryLib, which contains the glue code to access the MacOS. NameRegistryLib 
  592. is in the VideoToolbox Libs folder; add it to your project and use the pop-up 
  593. menu to select "import 'weak'".) As of 1/96, Apple has
  594. distributed NameRegistry.h and NameRegistryLib only as part of the Apple
  595. Developer Mailing and the SDK Subscription (either costs $250/year from APDA). These 
  596. two files are not included with Metrowerks CodeWarrior 8. You don't need
  597. NameRegistry.h, because Identify.c includes the needed definitions, but you do need
  598. NameRegistryLib, which is in the VideoToolbox Libs folder. Alternatively,
  599. you may set the flag NO_NAME_REGISTRY_LIB to true, which will disable the rest of 
  600. this file, but allows you to link and run your programs.
  601.  
  602. Once compiled and linked with the NameRegistryLib glue, this code will run on any
  603. PCI PowerMac. A temporary #if section takes care of the missing header file, so
  604. you'll be able to compile, but I don't think you'll be able to link this unless
  605. you have NameRegistryLib. I confess to being somewhat unsure on the last point
  606. since there are suggestions in the original source files that NameRegistryLib
  607. could be installed in the System:Extensions folder of the target machine instead
  608. of being linked into the application itself. That is apparently what the call
  609. below to Code Fragment Manager is for. However, I don't really understand this
  610. arcane stuff (i.e. "weak linking"), and it didn't seem worth learning for the
  611. routines in this file. Metrowerks has a "weak-linking" option, but limited
  612. experimentation suggests that you have to have the library in order to weak-link
  613. to it, which is no help at all for someone who doesn't have the library.
  614.  
  615. HISTORY:
  616. 10/13/95 dgp wrote GetVideoProperties() and GetCPUProperties(), based on general
  617.         advice from Apple (below) and 
  618.         the PCISlotsUtils.cp source file for Apple's "PCI Slot Peek" demo,
  619.         in the PCI developer's kit included on the September '95 Developer's CD. 
  620.  
  621. Hi Denis
  622.  
  623. Thank you for your link (ID# 151426) concerning how to detect a L2 cache and
  624. get the processor speed.
  625.  
  626. Let's look at the cache first.  Before the introduction of the PCI Power
  627. Macintosh computers there was no API to detect the presents of this cache.  On
  628. a PCI Macintosh there is the Name Registry.  This registry contains this
  629. information in the form of a property.  The property name is "name" and if
  630. there is a L2 cache the property value is "l2-cache".  Look at the Developer CD
  631. for Sept 95.  In the What's New folder there is the PCI DDK.  There is a
  632. program called Display Name Registry that will show what I have just explained.
  633. Of course it must run on a PCI machine.  There is also a document called
  634. "Designing PCI Cards&Drivers...." that shows examples of using the Name
  635. Registry to search for system resources.
  636.  
  637. Now the CPU speed.  Like the last case, before PCI Macs there was no direct
  638. call that could be made to determine CPU speed.  There are Gestalt calls for
  639. CPU type if that's any help.  However, there are CPU accelerator cards so that
  640. would present a problem.  The two calls you mentioned (GetCPUSPeed &
  641. MaximumProcessSpeed) are both for portable Macs that use the Power Manager.
  642. The traps for these calls are not implemented on other Macs.  At least not the
  643. 9500 that I tried.  However, PCI Macs and the Name Registry contain this
  644. information also.  Again go to Display Name Registry and look at the
  645. Devices:device-tree;PowerPC,604 property.  Expand it and there is an additional
  646. sub property called "clock-frequency".  My 9500 show 132000000.
  647.  
  648. So you have what you want but starting with PCI Macs.
  649.  
  650. Regards
  651.  
  652. Wayne Flansburg
  653. Developer Technical Support
  654. Apple Computer
  655. 12 October 1995
  656. */
  657.  
  658. #if UNIVERSAL_HEADERS>1
  659.     #ifndef __CODEFRAGMENTS__
  660.         #include <CodeFragments.h>
  661.     #endif
  662. #else
  663.     #undef NO_NAME_REGISTRY_LIB
  664.     #define NO_NAME_REGISTRY_LIB 1
  665. #endif
  666. // The version macro UNIVERSAL_INTERFACES_VERSION is defined in Apple's ConditionalMacros.h,
  667. // but only since version 2.1 (which was distributed with CodeWarrior 8).
  668. #if UNIVERSAL_INTERFACES_VERSION>=0x0212
  669.     #ifndef __NAMEREGISTRY__
  670.         #include <NameRegistry.h>
  671.     #endif
  672. #else
  673.     #if !defined(NO_NAME_REGISTRY_LIB) && !defined(__NAMEREGISTRY__)
  674.         // As of 10/13/95, Apple has distributed NameRegistry.h and NameRegistryLib only to 
  675.         // Apple Developers. Neither is included with Metrowerks CodeWarrior 7.
  676.         // The following definitions and prototypes, copied from NameRegistry.h, are just 
  677.         // enough to allow compilation of this file without the header file. However, 
  678.         // you'll still need NameRegistryLib in order to link. 
  679.         // It's in the VideoToolbox Lib folder.
  680.         typedef UInt32 RegIterationOp;
  681.         typedef RegIterationOp RegEntryIterationOp;
  682.         struct RegEntryIter {
  683.             void *opaque;
  684.         };
  685.         typedef struct RegEntryIter *RegEntryIterPtr;
  686.         typedef struct RegEntryIter RegEntryIter;
  687.         struct RegEntryID {
  688.             UInt8 opaque[16];
  689.         };
  690.         typedef struct RegEntryID RegEntryID, *RegEntryIDPtr;
  691.         typedef char RegCStrPathName;
  692.         typedef UInt32 RegPathNameSize;
  693.         enum {
  694.             kRegMaximumPropertyNameLength = 31,                            /* Max length of Property Name */
  695.             kRegPropertyNameTerminator    = 0x00,                            /* '\0' */
  696.             kRegIterContinue = 0x1UL
  697.         };
  698.         typedef char RegPropertyName, *RegPropertyNamePtr, RegPropertyNameBuf[kRegMaximumPropertyNameLength + 1];
  699.         typedef UInt32 RegPropertyValueSize;
  700.         extern OSStatus RegistryEntryIterateCreate(RegEntryIter *cookie);
  701.         extern OSStatus RegistryEntryIterateDispose(RegEntryIter *cookie);
  702.         extern OSStatus RegistryEntryIterate(RegEntryIter *cookie, RegEntryIterationOp relationship, RegEntryID *foundEntry, Boolean *done);
  703.         extern OSStatus RegistryEntryToPathSize(const RegEntryID *entryID, RegPathNameSize *pathSize);
  704.         extern OSStatus RegistryCStrEntryToPath(const RegEntryID *entryID, RegCStrPathName *pathName, RegPathNameSize pathSize);
  705.         extern OSStatus RegistryPropertyGetSize(const RegEntryID *entryID, const RegPropertyName *propertyName, RegPropertyValueSize *propertySize);
  706.         extern OSStatus RegistryPropertyGet(const RegEntryID *entryID, const RegPropertyName *propertyName, void *propertyValue, RegPropertyValueSize *propertySize);
  707.         extern OSStatus RegistryEntryIDDispose(RegEntryID *id);
  708.     #endif
  709. #endif
  710. #include <stdio.h>
  711. #include <string.h>
  712. /*
  713.     RegPropertyModifiers propertyModifiers;
  714.     if(0 && !error){
  715.         error=RegistryPropertyGetMod(&entry,foundProperty,&propertyModifiers);
  716.     }
  717. */
  718.  
  719. void GetVideoProperties(GDHandle device,char *slotName,char *cardName,char *cardModel);
  720. void GetCPUProperties(char *cpuName,long *cpuHz,Boolean *hasL2Cache);
  721.  
  722. void GetVideoProperties(GDHandle device,char *slotName,char *cardName,char *cardModel)
  723. {
  724. #if NO_NAME_REGISTRY_LIB || !GENERATINGPOWERPC
  725.     device;
  726.     slotName[0]=0;
  727.     cardName[0]=0;
  728.     cardModel[0]=0;
  729.     return;
  730. #else
  731.     /*
  732.     Assume we're running native on ppc.
  733.     If this were compiled as 68k code, the calls to the NameRegistryLib, which is PPC code, 
  734.     would require intervention by the Mixed Mode Manager,
  735.     and I decided not to bother with that complication, since there seems little point in
  736.     compiling programs as 68k code to run on a PowerPC. TimeVideo is distributed as a fat 
  737.     binary, so it always runs native.
  738.     */
  739.     OSStatus error;
  740.     RegEntryIter cookie;
  741.     Boolean done;
  742.     RegEntryIterationOp    op;
  743.     RegEntryID entry;
  744.     RegCStrPathName *pathName;
  745.     RegPathNameSize pathNameSize;
  746.     RegPropertyNameBuf foundProperty;
  747.     RegPropertyValueSize propertySize;
  748.     long templong;
  749.     short driverRef;
  750.     
  751.     slotName[0]=0;
  752.     cardName[0]=0;
  753.     cardModel[0]=0;
  754.     if(device==NULL)return;
  755.     {
  756.         /*
  757.         To determine whether you're running on a PCI Mac, you must first check if the
  758.         NameRegistry exists.  There is a Gestalt Manager selector for
  759.         this, 'nreg'.  If it exists, the value returned will be the version number
  760.         (number 1 for now) of the Name Registry.  Otherwise, you should get a
  761.         gestaltUndefSelectorErr.  If the Name Registry does not exist then you should
  762.         assume that it is not a PCI based machine; it may have NuBus or no bus at all.
  763.         */
  764.         error=Gestalt(gestaltNameRegistryVersion,&templong);
  765.         if(error)return;    // No PCI slots
  766.         /*
  767.         *** I didn't add this yet --- dgp ***
  768.         You can use the Name Registry to determine if a PCI bus exists.  To do this you
  769.         should use RegistryEntrySearch to locate an entry/node having a known
  770.         property/value, propertyName = "devic-type", and propertyValue = "pci"  If an
  771.         entry is found that has that known property/value (noErr and done = "False"),
  772.         there is a PCI bus.
  773.         Wayne Flansburg
  774.         DTS
  775.         Apple Computer
  776.         1/26/96
  777.         */
  778.     }
  779.     {
  780.         // If NameRegistryLib is weak-linked, it may be missing at runtime, so we
  781.         // check for the library, to prevent a crash if we try to access the library's exports.
  782.         CFragConnectionID connID;
  783.         Ptr mainAddr;
  784.         Str255 errName;
  785.         
  786.         error=Gestalt(gestaltCFMAttr,&templong);    // Code Fragment Manager?
  787.         if(!error)error=GetSharedLibrary((ConstStr63Param)"\pNameRegistryLib"
  788.             ,kAnyCFragArch,kFindCFrag,&connID,&mainAddr,errName);
  789.         if(error)return;    // No NameRegistryLib
  790.     }
  791.     op=kRegIterContinue;
  792.     error=RegistryEntryIterateCreate(&cookie);
  793.     if(!error){
  794.         done=FALSE;
  795.         while (!error && !done){
  796.             error=RegistryEntryIterate(&cookie,op,&entry,&done);
  797.             if(!error && !done){
  798.                 pathName=NULL;
  799.                 error=RegistryEntryToPathSize(&entry,&pathNameSize);
  800.                 if(!error){
  801.                     pathName=(RegCStrPathName *) NewPtr(pathNameSize);
  802.                     if(pathName==NULL)error=MemError();
  803.                 }
  804.                 if(!error){
  805.                     error=RegistryCStrEntryToPath(&entry,pathName,pathNameSize);
  806.                     //printf("%s\n",pathName);    // debugging
  807.                 }
  808.                 if(!error){
  809.                     // Found a video device
  810.                     strcpy(foundProperty,"driver-ref");
  811.                     propertySize=0;
  812.                     error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  813.                     if(!error){
  814.                         assert(propertySize==sizeof(driverRef));
  815.                         error=RegistryPropertyGet(&entry,foundProperty,&driverRef,&propertySize);
  816.                         if(!error && driverRef==(*device)->gdRefNum){
  817.                             done=TRUE; // we found the correct driver
  818.                             strcpy(foundProperty,"name");
  819.                             propertySize=0;
  820.                             error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  821.                             if(!error)error=RegistryPropertyGet(&entry,foundProperty,cardName,&propertySize);
  822.                             strcpy(foundProperty,"model");
  823.                             propertySize=0;
  824.                             error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  825.                             if(!error)error=RegistryPropertyGet(&entry,foundProperty,cardModel,&propertySize);
  826.                             propertySize=0;
  827.                             strcpy(foundProperty,"AAPL,slot-name");
  828.                             error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  829.                             if(!error)error=RegistryPropertyGet(&entry,foundProperty,slotName,&propertySize);
  830.                             // PCI slot names on Power Macintosh currently are of form : A1,B1,C1,D2,E2,F2
  831.                         }
  832.                     }else error=noErr;    // there is a card but NO display attached (no open driver)
  833.                 }
  834.                 if(pathName != NULL)DisposePtr((Ptr) pathName);
  835.                 RegistryEntryIDDispose(&entry);
  836.             }
  837.         }
  838.         RegistryEntryIterateDispose(&cookie);
  839.     }
  840.     return;
  841. #endif
  842. }    /* GetVideoProperties */
  843.  
  844. void GetCPUProperties(char *cpuName,long *cpuHz,Boolean *hasL2Cache)
  845. {
  846. #if NO_NAME_REGISTRY_LIB || !GENERATINGPOWERPC
  847.     cpuName[0]=0;
  848.     *cpuHz=0;
  849.     *hasL2Cache=0;
  850.     return;
  851. #else
  852.     /*
  853.     Assume we're running native on ppc.
  854.     If this were compiled as 68k code, the calls to the NameRegistryLib, which is PPC code, 
  855.     would require intervention by the Mixed Mode Manager,
  856.     and I decided not to bother with that complication, since there seems little point in
  857.     compiling programs as 68k code to run on a PowerPC. TimeVideo is distributed as a fat 
  858.     binary, so it always runs native.
  859.     */
  860.     OSStatus error;
  861.     RegEntryIter cookie;
  862.     Boolean done;
  863.     RegEntryIterationOp    op;
  864.     RegEntryID entry;
  865.     RegCStrPathName *pathName;
  866.     RegPathNameSize pathNameSize;
  867.     RegPropertyNameBuf foundProperty;
  868.     RegPropertyValueSize propertySize;
  869.     long templong;
  870.     char *propertyValue[256];
  871.     
  872.     cpuName[0]=0;
  873.     *cpuHz=0;
  874.     *hasL2Cache=0;
  875.     {
  876.         // If the NameRegistryLib is weak-linked, and it is missing, we need to
  877.         // check for the library, to prevent a crash if we try to access the library's exports;
  878.         // currently the Finder reports if the library is missing
  879.         CFragConnectionID        connID;
  880.         Ptr                        mainAddr;
  881.         Str255                    errName;
  882.         
  883.         error=Gestalt(gestaltCFMAttr,&templong);    // Code Fragment Manager?
  884.         if(!error)error=GetSharedLibrary((ConstStr63Param)"\pNameRegistryLib"
  885.             ,kAnyCFragArch,kFindCFrag,&connID,&mainAddr,errName);
  886.         if(error)return;    // No NameRegistryLib
  887.     }
  888.     error=Gestalt(gestaltNameRegistryVersion,&templong);
  889.     if(error)return;    // No PCI slots
  890.  
  891.     op=kRegIterContinue;
  892.     error=RegistryEntryIterateCreate(&cookie);
  893.     if(!error){
  894.         done=FALSE;
  895.         while (!error && !done){
  896.             error=RegistryEntryIterate(&cookie,op,&entry,&done);
  897.             if(!error && !done){
  898.                 pathName=NULL;
  899.                 error=RegistryEntryToPathSize(&entry,&pathNameSize);
  900.                 if(!error){
  901.                     pathName=(RegCStrPathName *) NewPtr(pathNameSize);
  902.                     if(pathName==NULL)error=MemError();
  903.                 }
  904.                 if(!error){
  905.                     error=RegistryCStrEntryToPath(&entry,pathName,pathNameSize);
  906.                     //printf("%s\n",pathName);    // debugging
  907.                 }
  908.                 if(!error){
  909.                     propertySize=0;
  910.                     strcpy(foundProperty,"device_type");
  911.                     error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  912.                     if(!error)assert(propertySize<=sizeof(propertyValue));
  913.                     if(!error){
  914.                         error=RegistryPropertyGet(&entry,foundProperty,propertyValue,&propertySize);
  915.                         // Is it what we want?
  916.                         if(!error && strcmp("cpu",(char *)propertyValue)!=0)error=-1; // no
  917.                     }
  918.                     if(!error){
  919.                         strcpy(foundProperty,"clock-frequency");
  920.                         error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  921.                     }
  922.                     if(!error)assert(propertySize==sizeof(*cpuHz));
  923.                     if(!error){
  924.                         error=RegistryPropertyGet(&entry,foundProperty,cpuHz,&propertySize);
  925.                     }
  926.                     if(!error){
  927.                         strcpy(foundProperty,"name");
  928.                         error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  929.                     }
  930.                     if(!error)assert(propertySize<256);
  931.                     if(!error){
  932.                         error=RegistryPropertyGet(&entry,foundProperty,cpuName,&propertySize);
  933.                     }
  934.                     if(!error){
  935.                         strcpy(foundProperty,"l2-cache");
  936.                         error=RegistryPropertyGetSize(&entry,foundProperty,&propertySize);
  937.                         if(!error)*hasL2Cache=1;
  938.                         else *hasL2Cache=0;
  939.                     }
  940.                     error=0;
  941.                 }
  942.                 if(pathName != NULL)DisposePtr((Ptr) pathName);
  943.                 RegistryEntryIDDispose(&entry);
  944.             }
  945.         }
  946.         RegistryEntryIterateDispose(&cookie);
  947.     }
  948.     return;
  949. #endif
  950. }    /* GetCPUProperties */
  951.